import { useMessage } from '@hooks/useAppMessage'
import { useAxios } from '@hooks/useAxios'
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { API_NAVBAR_URL } from './baseApi'

export const useNavbarQuery = (params) => {
  const { http } = useAxios()
  return useQuery({
    queryKey: ['navbar.list', { ...params }],
    queryFn: async () => {
      return await http.get(API_NAVBAR_URL, { params })
    },
  })
}

export const useNavbarDetailQuery = (id) => {
  const { http } = useAxios()
  return useQuery({
    queryKey: ['navbar.detail', id],
    queryFn: async () => {
      const response = await http.get(`${API_NAVBAR_URL}/${id}`)
      return response.data
    },
    staleTime: 1000 * 60 * 5, // 5 minutes
    enabled: !!id,
  })
}

export const useNavbarUpdateMutation = () => {
  const { http } = useAxios()
  const queryClient = useQueryClient()
  const message = useMessage()
  return useMutation({
    mutationFn: async (data) => await http.put(`${API_NAVBAR_URL}/${data.id}`, data),
    onSuccess: (data) => {
      queryClient.invalidateQueries(['navbar.list'])
      message.success('更新成功')
    },
  })
}
